package git.soulbgm.common.pojo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import git.soulbgm.common.deserializer.CustomDateDeserializer;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 查询dto
 *
 * @author SoulBGM
 * @date 2024/04/01
 */
public class QueryDto<T> {

    private Long startTimestamp;
    @JsonDeserialize(using = CustomDateDeserializer.class)
    private Date startTime;
    private Long endTimestamp;
    @JsonDeserialize(using = CustomDateDeserializer.class)
    private Date endTime;
    private Integer current;
    private Integer size;
    private Integer offset;
    private Long id;
    private String ids;
    private List<Long> idList;
    @JsonUnwrapped
    private T data;

    public Long getStartTimestamp() {
        if (startTimestamp == null && startTime != null) {
            return startTime.getTime();
        }
        return startTimestamp;
    }

    public void setStartTimestamp(Long startTimestamp) {
        this.startTimestamp = startTimestamp;
    }

    public Date getStartTime() {
        if (startTime == null && startTimestamp != null) {
            return new Date(startTimestamp);
        }
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Long getEndTimestamp() {
        if (endTimestamp == null && endTime != null) {
            return endTime.getTime();
        }
        return endTimestamp;
    }

    public void setEndTimestamp(Long endTimestamp) {
        this.endTimestamp = endTimestamp;
    }

    public Date getEndTime() {
        if (endTime == null && endTimestamp != null) {
            return new Date(endTimestamp);
        }
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public Integer getCurrent() {
        return current;
    }

    public void setCurrent(Integer current) {
        this.current = current;
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }

    public Integer getOffset() {
        if (offset == null && current != null && size != null) {
            return (current - 1) * size;
        }
        return offset;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getIds() {
        if (CharSequenceUtil.isBlank(ids) && CollUtil.isNotEmpty(idList)) {
            return idList.stream().map(Object::toString).collect(Collectors.joining(","));
        }
        return ids;
    }

    public void setIds(String ids) {
        this.ids = ids;
    }

    public List<Long> getIdList() {
        if (idList == null && CharSequenceUtil.isNotBlank(ids)) {
            return Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        }
        return idList;
    }

    public void setIdList(List<Long> idList) {
        this.idList = idList;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
